package com.iengoo.servlet.dao.impl;

import com.iengoo.servlet.config.DBConfig;
import com.iengoo.servlet.dao.TicketDao;
import com.iengoo.servlet.entity.Ticket;
import com.iengoo.servlet.model.Page;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-01-29 11:17
 */
public class TicketDaoImpl implements TicketDao {
    @Override
    public List<Ticket> query(Page page) {
        // 泛型
        List<Ticket> ticketList = new ArrayList<>();
        try {
            Class.forName(DBConfig.driver);
            String querySql = "SELECT id, start_time, start_station,stop_station,price,surplus_ticket,type FROM ticket " +
                    "limit ?, ?";
            Connection conn = DriverManager.getConnection(DBConfig.url, DBConfig.username, DBConfig.password);
            PreparedStatement ps = conn.prepareStatement(querySql);
            ps.setInt(1, page.getLimit());
            ps.setInt(2, page.getSize());
            ResultSet set = ps.executeQuery();

            while (set.next()) {
                Ticket tmpTicket = new Ticket();
                tmpTicket.setId(set.getInt("id"));
                tmpTicket.setStartTime(set.getTime("start_time"));
                tmpTicket.setStartStation(set.getString("start_station"));
                tmpTicket.setStopStation(set.getString("stop_station"));
                tmpTicket.setPrice(set.getInt("price"));
                tmpTicket.setSurplusTicket(set.getInt("surplus_ticket"));
                tmpTicket.setType(set.getString("type"));
                ticketList.add(tmpTicket);
            }

            set.close();
            ps.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ticketList;
    }

    @Override
    public void create(Ticket ticket) {

    }

    @Override
    public void delete(int id) {

    }

    @Override
    public void update(Ticket ticket) {

    }

    @Override
    public Ticket findById(int id) {
        return null;
    }
}
